package donot996.icu.tieba.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import donot996.icu.tieba.domain.DetailList;
import donot996.icu.tieba.domain.TopicList;
import donot996.icu.tieba.mapper.DetailListMapper;
import donot996.icu.tieba.mapper.TopicListMapper;
import donot996.icu.tieba.service.IDetailListService;
import donot996.icu.tieba.service.ITopicListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;

@CrossOrigin
@Service
public class DetailListServiceImpl extends ServiceImpl<DetailListMapper,DetailList> implements IDetailListService {
    @Autowired
    DetailListMapper detailListMapper;

    @Autowired
    TopicListMapper topicListMapper;

    @Override
    public void replay(Long topicId,Long userId, String userName, String content) {
        // 先往detail表中插入 返回=> detailId
        DetailList detailList = new DetailList();
        detailList.setContent(content);
        detailList.setUserId(userId);
        detailList.setUserName(userName);
        detailListMapper.insert(detailList);

        Long detailListId = detailList.getId();

        //  根据topicId 查出topic => 获取 detailIds 拼接上detailId
        TopicList topicList = topicListMapper.selectById(topicId);
        String detailIds = topicList.getDetailIds();
        topicList.setDetailIds("".equals(detailIds) ? String.valueOf(detailListId) : (detailIds +"," + String.valueOf(detailListId)));
        topicListMapper.updateById(topicList);
    }
}
